package utils;

import entity.Book;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;

public class FileAppenderUtils {

    private BufferedWriter bufferedWriter ;
    private String filepath;

    //初始化输出流，保证输出流只有一个对象
    public FileAppenderUtils(String filepath) throws IOException {
        this.filepath = filepath;
        this.bufferedWriter = new BufferedWriter(new FileWriter(filepath,true));
    }


    //输出流到本地文件books.txt
    public Integer FileWriterFile(List<String> data) throws IOException {

        int count = 0;
        for (String line : data) {
            bufferedWriter.write(line);
            bufferedWriter.newLine();
            count++;
        }
        return count;

    }

    public Integer FileWriterFiles(String s) throws IOException {

        int count = 0;
            bufferedWriter.write(s);
            bufferedWriter.newLine();
            count++;
        return count;

    }


    public  void close() throws IOException {
        bufferedWriter.close();
    }
}

//    public static void main(String[] args) throws IOException {
//        Book book = new Book();
//        FileAppenderUtils fileAppenderUtils = new FileAppenderUtils("D:\\books.txt");
//        Integer integer = null;
//        for (int i =0;i<=100;i++) {
//            List<String> list = Book.listToCSVString(5000);
//            integer = fileAppenderUtils.FileWriterFile(list);
//        }
//        System.out.println(integer);
//        FileAppenderUtils.close();
//    }
//}
